package org.neframework.webtest.base;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import com.tmsps.neframework.core.common.page.Page;

public class PageTag extends TagSupport {
	private static final long serialVersionUID = 1L;

	private Page page;

	private String form_name = "form1";// 页面表单名称
	private String page_param = "page";// 页面page参数名称

	private boolean is_ajax;
	private String ajax_id = "ajax_id";

	// 上一页
	private final String prev_model = "<li class=\"prev\"><a href=\"javascript:pageoption('@prev')\"> << </a></li>";
	private final String prev_disabled = "<li class=\"prev disabled\"><a href=\"javascript:void('0');\"> << </a></li>";
	// 页面
	private final String page_str = "<li><a href='javascript:pageoption(@pageNum)'>@pageNum</a></li>";
	private final String page_str_disabled_model = "<li class='active'><a href='javascript:(void(0))'>@pageNum</a></li>";
	// 下一页
	private final String next_model = "<li class=\"next\"><a href=\"javascript:pageoption('@next')\"> >> </a></li>";
	private final String next_disabled = "<li class=\"next disabled\"><a href=\"javascript:void('0');\"> >> </a></li>";
	// 共多少页
	private final String total_model = "<span style=\"line-height: 26px;padding:14px;\">共 @total条<span>";
	// ...
	private final String dots = "<li class='disabled'><a href='javascript:(void(0))'>...</a></li>";
	// page num hidden
	private final String hidden_model = "<input type='hidden' name='@pageParam.pageNum' value='@pageNum'>";

	// @Override
	public int doStartTag() throws JspException {
		JspWriter out = this.pageContext.getOut();

		if (page == null) {
			page = (Page) this.pageContext.getRequest().getAttribute("page");
		}
		if (page == null) {
			return SKIP_BODY;
		}

		String pageContent = this.getPageLinkBottom(page);

		try {
			out.print(pageContent);
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return SKIP_BODY;
	}

	public String getPageLinkBottom(Page page) {

		if (page.pageSize <= 0 && page.pageNum <= 0) {
			return "";
		}
		// 处理页码
		String prev = prev_model.replace("@prev", (page.getPageNum() - 1) + "");
		// page_str = page_str.replace("@pageNum", page.getPageNum() + "");
		String page_str_disabled = page_str_disabled_model.replace("@pageNum",
				page.getPageNum() + "");
		String next = next_model.replace("@next", (page.getPageNum() + 1) + "");
		String total = total_model.replace("@total", page.getRowTotal() + "");
		String hidden = hidden_model.replace("@pageParam", this.page_param)
				.replace("@pageNum", page.getPageNum() + "");

		StringBuffer tmpbottom = new StringBuffer();
		StringBuffer tmpoptions = new StringBuffer();

		int pageTotal = page.getPageTotal();
		int pageNum = page.getPageNum();

		if (pageTotal == 1) {
			// 只有一页情况下不显示页码信息
			// tmpoptions.append("<li class='current'>" + 1 + " </li>");
		} else if (pageTotal <= 10) {// 显示页数小于10

			tmpoptions.append("<ul class='pagination pagination-sm'>");
			if (pageNum != 1) {
				tmpoptions.append(prev);
			} else {
				tmpoptions.append(prev_disabled);
			}
			for (int i = 1; i <= page.pageTotal; i++) {
				if (i == pageNum) {
					tmpoptions.append(page_str_disabled);
				} else {
					tmpoptions.append(page_str.replace("@pageNum", i + ""));
				}
			}
			if (pageNum != pageTotal) {
				tmpoptions.append(next);
			} else {
				tmpoptions.append(next_disabled);
			}
			tmpoptions.append(total);
			tmpoptions.append("</ul>");
		} else {// 显示页数大于10
			boolean signleft = false;// 标志，是否已增加省略符号
			boolean signright = false;// 标志，是否已增加省略符号
			tmpoptions.append("<ul class='pagination pagination-sm'>");
			if (pageNum != 1) {
				tmpoptions.append(prev);
			} else {
				tmpoptions.append(prev_disabled);
			}
			for (int i = 1; i <= page.pageTotal; i++) {
				if (i < page.pageNum) {
					if (i <= 3) {// 显示前三页
						tmpoptions.append(page_str.replace("@pageNum", i + ""));
					} else {
						if (i >= (pageNum - 2)) {// 该页为当前页面的前页
							tmpoptions.append(page_str.replace("@pageNum", i
									+ ""));
						} else {
							if (pageNum == i + 3 && i == 4) {
								tmpoptions.append(page_str.replace("@pageNum",
										i + ""));
								signleft = true;
							} else if (!signleft) {// 未添加省略符号
								tmpoptions.append(dots);
								signleft = true;
							}
						}
					}
				}
				if (i == page.pageNum) {
					tmpoptions.append(page_str_disabled);
				}
				if (i > page.pageNum) {
					if (i <= (pageNum + 2)) {
						tmpoptions.append(page_str.replace("@pageNum", i + ""));
					} else {
						if (i >= (pageTotal - 2)) {
							tmpoptions.append(page_str.replace("@pageNum", i
									+ ""));
						} else {
							if (((pageNum + 3) == i) && ((i + 3) == pageTotal)) {
								tmpoptions.append(page_str.replace("@pageNum",
										i + ""));
								signright = true;
							}
							if (!signright) {
								tmpoptions.append(dots);
								signright = true;
							}
						}
					}
				}
			}
			if (pageNum != pageTotal) {
				tmpoptions.append(next);
			} else {
				tmpoptions.append(next_disabled);
			}
			tmpoptions.append(total);
			tmpoptions.append("</ul>");
		}

		tmpbottom.append(tmpoptions.toString());

		tmpbottom.append(hidden);
		tmpbottom.append("<script type='text/javascript'>");

		if (is_ajax) {
			tmpbottom.append(getAjaxPageFunc(page));
		} else {
			tmpbottom.append(getPageFunc(page));
		}

		tmpbottom.append("</script>");

		return tmpbottom.toString().replaceAll("pageoption",
				"pageoption_" + this.form_name);

	}

	private String getPageFunc(Page page) {
		// 下拉列表翻页方法
		String func = "function pageoption(option){$(\"input[name='@page_param.pageNum']\").val(option);$('#@formName').submit();}";
		func = func.replace("@page_param", this.page_param);
		func = func.replace("@formName", this.form_name);
		return func;
	}

	private String getAjaxPageFunc(Page page) {
		// ajax 分页函数
		String func = "function pageoption(option){$(\"input[name='@page_param.pageNum']\").val(option);var params=$('#@formName').serialize();var url=$('#@formName').attr('action')+'?'+params;$('#@ajax_id').load(url);}";
		func = func.replace("@page_param", this.page_param);
		func = func.replace("@formName", this.form_name);
		func = func.replace("@ajax_id", ajax_id);
		return func;
	}

	// ====== get / set =================
	public void setPage(Page page) {
		this.page = page;
	}

	public void setIs_ajax(boolean is_ajax) {
		this.is_ajax = is_ajax;
	}

	public void setAjax_id(String ajax_id) {
		this.ajax_id = ajax_id;
	}

}
